﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OurFAS.Data;
using GetHashValue;

namespace OurFAS.MySystem
{
    public partial class FrmAccountDetail : Form
    {  
        #region 成员变量
        private string _sysAccount = "admin";
        private Base_Account _account;
        private EnumHelper.EnumObjctOperateType _opType;
        #endregion

        #region  构造函数
        public FrmAccountDetail(EnumHelper.EnumObjctOperateType opType ,Base_Account account)
        {
            InitializeComponent();
            _account = account;
            _opType = opType;
        }
        #endregion


        #region  控件函数
        private void FrmAccountDetail_Load(object sender, EventArgs e)
        {
            //初始化Combobox等控件的绑定内容
            OurFasDBEntities entity = new OurFasDBEntities();
           
            cmbRole.DataSource = null;
            cmbRole.DataSource = entity.Base_Role.ToList();
            
            //
            switch (_opType)
            {
                case EnumHelper.EnumObjctOperateType.New:
                    {
                        //控件可用性设置
                        tbPwd.ReadOnly = false;
                        cbIsFreeze.Checked = false;
                    }
                    break;
                case EnumHelper.EnumObjctOperateType.Edit:
                    {
                        tbPwd.ReadOnly = true;
                        tbUserCode.ReadOnly = true;
                        //写入对象内容
                        tbUserCode.Text = _account.UserCode;
                        tbUserName.Text = _account.UserName;
                        cmbRole.SelectedValue = _account.UserRole.Trim();
                        tbDept.Text = _account.UserDept;
                        tbRemark.Text = _account.UserRemark;
                        cbIsFreeze.Checked = _account.UserFreeze;

                        if (_account.UserCode.Trim() == _sysAccount)
                        {
                            cbIsFreeze.Enabled = false;
                        }
                    }
                    break;
            }

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性判断
            if (string.IsNullOrWhiteSpace(tbUserCode.Text))
            {
                MessageBox.Show("用户登录名不能为空!");
                return;
            }

            if (cmbRole.SelectedIndex == -1)
            {
                MessageBox.Show("请选择用户角色!");
                return;
            }

            if (_opType == EnumHelper.EnumObjctOperateType.New)
            {
                Base_Account rec = new Base_Account();
                rec.UserCode = tbUserCode.Text;
                rec.UserName = tbUserName.Text;
                rec.UserPwd = GetHashValue.Cryptography.GetHashValue(tbPwd.Text.Trim());
                Base_Role role = cmbRole.SelectedItem as Base_Role;
                rec.UserRole = role.RoleCode;
                rec.UserDept = tbDept.Text;
                rec.UserRemark = tbRemark.Text;
                rec.UserFreeze = cbIsFreeze.Checked;

                try
                {
                    OurFasDBEntities entity = new OurFasDBEntities();
                    entity.Base_Account.AddObject(rec);
                    entity.SaveChanges();
                    MessageBox.Show("新建用户成功！");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("新建用户失败！");
                }
            }
            else if (_opType == EnumHelper.EnumObjctOperateType.Edit)
            {
                OurFasDBEntities entity = new OurFasDBEntities();
                Base_Account rec = entity.Base_Account.Where(c => c.UserCode.Trim() == _account.UserCode).FirstOrDefault();
                if (rec != null)
                {
                    rec.UserCode = tbUserCode.Text;
                    rec.UserName = tbUserName.Text;
                    Base_Role role = cmbRole.SelectedItem as Base_Role;
                    rec.UserRole = role.RoleCode;
                    rec.UserDept = tbDept.Text;
                    rec.UserRemark = tbRemark.Text;
                    rec.UserFreeze = cbIsFreeze.Checked;
                    try
                    {
                        entity.SaveChanges();
                        MessageBox.Show("保存用户信息成功！");
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    catch
                    {
                        MessageBox.Show("保存用户信息失败！");
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion
    }
}
